class Comic < ActiveRecord::Base
  belongs_to :story
  has_attachment 	:content_type=>:image,
					:storage=>:file_system,
					#:max_size => 15.megabyte,
					:size => 0.kilobytes..15.megabyte,			
                    :thumbnails => { :large=>'280x417!', :medium => '130x193!', :small => '70x104!',  :mini => '35x52!' },
                    :processor => 'Rmagick',
					:thumbnail_class=>Thumbnail
  validates_as_attachment
  validates_uniqueness_of :rank
 
  def before_create
    if s = Comic.most_recent_page
	  self.rank = s.rank+1
	else
	  self.rank = 1
	end
  end
 
# this doesn't work on bluehost 
#  def after_create
#	update_attribute("rank",id) if self.rank == nil
#  end
  
  def rank=(num)
    @prev = nil
	@next = nil
	write_attribute(:rank, num)
  end
  
  def prev
    return @prev if @prev
    @prev = Comic.find(:first, :conditions=>["rank < ?",rank],:order=> 'rank desc')
  end
  
  def next
    return nil if @next == -1;
	return @next if @next
    @next = Comic.find(:first, :conditions=>["rank > ?",rank],:order=> 'rank asc')  
  end
  
  def self.most_recent_page    
  	@next = -1
    self.find(:first, :order=> 'rank desc')	
  end

end
